<?php
class Ainte extends Integral{

    public function Ainte($member_id){
	    $this->member_id = $member_id;
		$this->initialize();
	}
    //更详细地计算积分数量
    public function giveIntegralMore($num,$ruleid,$memberid,$remark,$rewarderid,$time,$siteid,$approvername){
	    $integral_numberDB = M('integral_number');
        $datenum = $integral_numberDB->where(array('member_id'=>$memberid,'typename'=>'Ainte'))->find();
        $typeid=$datenum['type_id'];
        if($memberid&&$datenum){
            $number = $datenum['number']+$num;
            $mes = $integral_numberDB->where(array('id'=>$datenum['id']))->save(array('number'=>$number));
        }else{
            $typeid=M('integral_type')->where(array('typename'=>'Ainte','site_id'=>$siteid))->getField('id');
            if($typeid){
                $integral_numberDB->add(array('type_id'=>$typeid,'typename'=>'Ainte','site_id'=>$siteid,'member_id'=>$memberid,'number'=>$num));
            }else{return false;}
        }
        if($mes){
            $remark = $remark==''?'赠送'.$datenum['remark'].'积分:'.$number:$remark;
            if($this->integral_logMore($memberid,$remark,$num,$typeid,$this->member_id,$ruleid,$rewarderid,$time,$siteid,$approvername)){
                return true;    
            }else{
                return false;    
            };
        }else{
            return false;
        }
      
	  return true;
	}
    //更新积分
    public function setIntegral($data){
	    $integral_numberDB = M('integral_number');
        $olddata = M('integral_log')->where(array('id'=>$data['id']))->find();
        $oldnum=$olddata['changenum'];
        $datenum = $integral_numberDB->where(array('member_id'=>$olddata['member_id'],'typename'=>'Ainte'))->find();
        $typeid=$datenum['type_id'];
        if($datenum&&isset($data['changenum'])){
            $number = $datenum['number']-$oldnum+$data['changenum'];
            $mes = $integral_numberDB->where(array('id'=>$datenum['id']))->save(array('number'=>$number));
        }else{
            $mes=$datenum?false:true;    
        }
        if($mes){
            $data['type_id']=$typeid;
            $data['giver_id']=$this->member_id;
            if($this->integral_logUpdate($data)){
                return true;    
            }else{
                return false;    
            };
        }else{
            return false;
        }
	    return true;
	}
    //删除积分并扣除积分
    public function deleteIntegral($id){
	    $integral_numberDB = M('integral_number');
        $olddata = M('integral_log')->where(array('id'=>$id))->find();
        $oldnum=$olddata['changenum'];
        $datenum = $integral_numberDB->where(array('member_id'=>$olddata['member_id'],'type_id'=>$olddata['type_id']))->find();
        if($datenum){
            $number = $datenum['number']-$oldnum;
            $mes = $integral_numberDB->where(array('id'=>$datenum['id']))->save(array('number'=>$number));
        }else{
            return false;   
        }
        if($mes){
            if($this->integral_logDelete($id)){
                return true;    
            }else{
                return false;    
            };
        }else{
            return false;
        }
      
	  return true;
	}
    //删除记录
    public function integral_logDelete($id) {
        $integral_logDB=M('integral_log');
        $map['id'] = $id;
        $result = $integral_logDB->where($map)->delete();
        if (false !== $result) {
           return true;
        } else {
           return false;
        }
    }
	//生成积分日志
	/*
	   remark 原因
	   changenum 金额
	*/
	public function integral_logMore($member_id,$remark,$changenum,$type_id,$giver_id,$rule_id,$rewarder_id,$time,$site_id,$approvername){
	    $data['remark'] =$remark;
	    $data['type_id']=$type_id;
	    $data['giver_id']=$giver_id;
	    $data['rule_id']=$rule_id;
	    $data['site_id'] = $site_id;
	    $data['changenum'] = $changenum;
	    $data['member_id'] = $member_id;
        $data['rewarder_id']=$rewarder_id;
	    $data['time']  = $time;
        $data['integralname']=D('Integral_type')->where('id = '.$type_id)->getfield('remark',false);
        $member=D('member')->where('id = '.$member_id)->field('name,nickname')->find();
        $data['membername']=!empty($member['name'])?$member['name']:$member['nickname'];
        $data['approvername']=$approvername;
        $data['memberpositionname']=D('member')->where('id = '.$member_id)->getfield('memberpositionname',false);
	   $integral_logDB = M('integral_log');
	   $integral_logDB->add($data);
       return true;
	}
    //修改积分日志并计算积分数量
    public function integral_logUpdate($data=array()){
        $integral_logDB=M('integral_log');
        if ($data['id']) {
            unset($data['site_id'],$data['type_id']);
            $map['id'] = array('eq',$data['id']);
            if (!$integral_logDB->create($data)) {return false;}
            $result = $integral_logDB->save($data);
            if (false !== $result) {
                return true;
            } else {return false;}
        } else {
            return false;
        }
    }
   //计算消费积分抵现
	public function integral_pay_array(){
	   return false;
	}
		//积分转换
	public function integral_change($id,$typeid){
		$integral_typeDB=M('integral_type');
		$integral_logDB=M('integral_log');
		$integral_numberDB=M('integral_number');
		$map['id']=$id;
		$olddata=$integral_logDB->where($map)->find();
		$ratio=unserialize($integral_typeDB->where(['site_id'=>__SITEID__,'id'=>$olddata['type_id']])->getField('aimratio'));
		if(!isset($ratio['Binte'])){return false;}
		//计算换算出来的积分
		$newnum=$olddata['changenum']*(float)$ratio['Ainte'];
		
		//将积分和积分类型修改至积分日志表
		$data['id']=$olddata['id'];
		$data['changenum']=$newnum;
		if(!$this->integral_logUpdate($data)){return false;}
		
		//将当前用户的积分换算至用户总积分表
		$oldnumberdata=$integral_numberDB->where(['type_id'=>$typeid,'site_id'=>__SITEID__])->find();
		$newnumber=$oldnumberdata['number']-$olddata['changenum']+$newnum;
		$numberdata['id']=$oldnumberdata['id'];
		$numberdata['number']=$newnumber;
		$result=$integral_numberDB->save($numberdata);
		if(!$result){return false;}
		
		return true;
	}

}